package com.zerdoor.controller;

import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.AddValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 增值服务管理
 * Created by xiaoji on 2015/1/29.
 */
@Controller
@RequestMapping("addValue")
public class AddValueController {
    @Autowired
    private AddValueService addValueService;

    /**
     * 跳转到增值服务页面
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){return "/addValue/index";}

    /**
     * FunName:           getAddValue
     * Description :      获取增值服务列表
     * @return String：返回增值服务数据
     * @Author:       xiaoji
     * @Create Date: 2014-11-24
     */
    @RequestMapping(value = "/getAddValue",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getAddValue(HttpServletRequest request,int page,int rows){

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        params.put("title",request.getParameter("title"));
        List<Map<String, Object>> list = addValueService.selectAllAddValue(params);
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows",list);
        result.put("total",pageInfo.getTotal());
        return result;
    }

    @RequestMapping(value = "/saveAddValue",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> saveAddValue(HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("addvalueId", null);
        params.put("price",new BigDecimal(request.getParameter("price")));
        params.put("nameCn",request.getParameter("nameCn"));
        params.put("nameEn",request.getParameter("nameCn"));
        params.put("sort",Integer.parseInt(request.getParameter("sort")));
        params.put("descriptionCn",request.getParameter("descriptionCn"));
        params.put("descriptionEn",request.getParameter("descriptionEn"));
        params.put("addvalueStatus",Integer.parseInt(request.getParameter("addvalueStatus")));
        try {
            addValueService.addAddValue(params);
        } catch (Exception e) {
            result.put("errormsg","添加增值服务失败");
        }
        return result;
    }

    @RequestMapping(value = "/updateAddValue",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> updateAddValue(HttpServletRequest request,int addvalueId){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("addvalueId", addvalueId);
        params.put("price",new BigDecimal(request.getParameter("price")));
        params.put("nameCn",request.getParameter("nameCn"));
        params.put("nameEn",request.getParameter("nameCn"));
        params.put("sort",Integer.parseInt(request.getParameter("sort")));
        params.put("descriptionCn",request.getParameter("descriptionCn"));
        params.put("descriptionEn",request.getParameter("descriptionEn"));
        params.put("addvalueStatus",Integer.parseInt(request.getParameter("addvalueStatus")));
        try {
            addValueService.updateAddValue(params);
        } catch (Exception e) {
            result.put("errormsg","添加增值服务失败");
        }
        return result;
    }

    /**
     * 激活
     * @return
     */
    @RequestMapping(value = "/activateAddValue",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> activateAddValue(Integer id){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("addvalueStatus",101001);
            params.put("addvalueId",id);
            addValueService.updateAddValueStatus(params);
        } catch (Exception e) {
            result.put("errormsg","添加增值服务失败");
        }
        return result;
    }

    /**
     * 停用
     * @return
     */
    @RequestMapping(value = "/stopAddValue",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> stopAddValue(Integer id){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("addvalueStatus",101002);
            params.put("addvalueId",id);
            addValueService.updateAddValueStatus(params);
        } catch (Exception e) {
            result.put("errormsg","添加增值服务失败");
        }
        return result;
    }

}
